package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 订单明细功能实现类
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的OrderDetail表mapper对象
     */
    @Autowired
    private OrderDetailMapper _OrderDetailMpper;

 			
    @Autowired
    private GoodMapper _GoodMapper;                        
			
 			
    @Autowired
    private OrderInfoMapper _OrderInfoMapper;                        
			

    /**
     * 订单明细分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<OrderDetailDto> List(OrderDetailPagedInput input) {

      
        //声明一个支持订单明细查询的(拉姆达)表达式
        LambdaQueryWrapper<OrderDetail> queryWrapper = Wrappers.<OrderDetail>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,OrderDetail::getId,input.getId())
                .eq(input.getCreatorId()!=null,OrderDetail::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入GoodId不为空,则进行精确查询
        if(input.getGoodId()!=null) {
            queryWrapper=queryWrapper.eq(OrderDetail::getGoodId,input.getGoodId());
       	 }
     	 //如果前端搜索传入GoodName不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getGoodName())) {
            queryWrapper=queryWrapper.eq(OrderDetail::getGoodName,input.getGoodName());
       	 }
     	 //如果前端搜索传入PriceStartRange,则要大于PriceStartRange输入的值
         if(input.getPriceStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(OrderDetail::getPrice,input.getPriceStartRange());
          }
         //如果前端搜索传入PriceEndRange,则要小于PriceEndRange输入的值
      		 if(input.getPriceEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(OrderDetail::getPrice,input.getPriceEndRange());
          }
     	 //如果前端搜索传入NumberStartRange,则要大于NumberStartRange输入的值
         if(input.getNumberStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(OrderDetail::getNumber,input.getNumberStartRange());
          }
         //如果前端搜索传入NumberEndRange,则要小于NumberEndRange输入的值
      		 if(input.getNumberEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(OrderDetail::getNumber,input.getNumberEndRange());
          }
     	 //如果前端搜索传入OrderInfoId不为空,则进行精确查询
        if(input.getOrderInfoId()!=null) {
            queryWrapper=queryWrapper.eq(OrderDetail::getOrderInfoId,input.getOrderInfoId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(OrderDetail::getCreationTime);
        //构建一个分页查询的model
        Page<OrderDetail> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取订单明细数据
        IPage<OrderDetail> pageRecords= _OrderDetailMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _OrderDetailMpper.selectCount(queryWrapper);

        //把OrderDetail实体转换成OrderDetail传输模型
        List<OrderDetailDto> items= Extension.copyBeanList(pageRecords.getRecords(),OrderDetailDto.class);
        for (OrderDetailDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的Good表信息
           GoodDto GoodDTO = new GoodDto();        
            Good  GoodEntity= _GoodMapper.selectOne(Wrappers.<Good>lambdaQuery().eq(Good::getId,item.getGoodId()));
           if(GoodEntity!=null) {  
          BeanUtils.copyProperties(GoodDTO,GoodEntity);
            item.setGoodDto(GoodDTO); 
           }
          
           
          	            
           //查询出关联的OrderInfo表信息
           OrderInfoDto OrderInfoDTO = new OrderInfoDto();        
            OrderInfo  OrderInfoEntity= _OrderInfoMapper.selectOne(Wrappers.<OrderInfo>lambdaQuery().eq(OrderInfo::getId,item.getOrderInfoId()));
           if(OrderInfoEntity!=null) {  
          BeanUtils.copyProperties(OrderInfoDTO,OrderInfoEntity);
            item.setOrderInfoDto(OrderInfoDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个订单明细查询
     */
    @SneakyThrows
    @Override
    public OrderDetailDto Get(OrderDetailPagedInput input) {
       if(input.getId()==null)
        {
            return new OrderDetailDto();
        }
        PagedResult<OrderDetailDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new OrderDetailDto();
    }

    /**
     *订单明细创建或者修改
     */
    @SneakyThrows
    @Override
    public OrderDetailDto CreateOrEdit(OrderDetailDto input) {

        //声明一个订单明细实体
        OrderDetail OrderDetail = new OrderDetail();

        //把前端传入的input参数拷贝到订单明细实体
        BeanUtils.copyProperties(OrderDetail,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(OrderDetail);

        //定义一个返回给前端的订单明细传输模型
        OrderDetailDto OrderDetailDto=new OrderDetailDto();

        //同理把操作的订单明细实体拷贝给订单明细传输模型
        BeanUtils.copyProperties(OrderDetailDto,OrderDetail);

        //把传输模型返回给前端
        return OrderDetailDto;
    }



}
